var outorderVue = new Vue({
	el : '#inorderPageDiv',
	data : {
		currentPage : 1,
		pageSize : 5,
		totalSize : 10,
		queryBtn : false,
		dialogFormVisible : false,
		labelPosition : 'left',
		orderVo : {
			outorderNum : '',
			fromWhereHouse : '',
			fromWherePosition : '',
			toWhereHouse : '',
			toWherePosition : '',
			toWhereLocation : '',
			goodsName : '',
			sku : '',
			pageNum : '',
			pageSize : ''
			
		},
		house: [],
		position : [],
		location : [],
		outorderDetail : [],
		goods: [],
		
		formLabelWidth : '120px',
		createOutorderForm : {
			goodsName : '',
			goodsSku : '',
			goodsDesc : '',
			goodsNum :'',
			goodsUnit : '',
			goodsPrice : '',
			goodsProduceDate : '',
			goodsPeriod : '',
			supplierName : '',
			toWhereHouse: '',
			toWhereHouseName : '',
			toWherePosition : '',
			toWherePositionName : '',
			toWhereLocation : '',
			toWhereLocationName : '',
			goodsTrades : [{
				id : '',
				goodsNum : ''
			}]
		},
		orderRules : {
			toWhereHouse :[
				{ required: true, message: '请选择仓库', trigger: 'blur' }
			],
			toWherePosition :[
				{ required: true, message: '请选择库位', trigger: 'blur' }
			],
			toWhereLocation : [
				{ required: true, message: '请选择货位', trigger: 'blur' }
			],
			goodsName : [
				{ required: true, message: '请输入商品名称', trigger: 'blur' },
			    {min : 1, max : 10,message:'需要在1~10个字符或文字之间'}
			],
			goodsSku : [
				{ required: true, message: '请输入商品编码', trigger: 'blur' },
			    {min : 1, max : 25,message:'需要在1~25个字符或文字之间'}
			],
			goodsDesc : [
				{ required: true, message: '请输入商品描述', trigger: 'blur' },
			    {min : 1, max : 15,message:'需要在1~15个字符或文字之间'}
			],
			goodsNum : [
				{ required: true, message: '请输入商品数量', type:'number',trigger: 'blur' }
			],
			goodsPrice : [
				{ required: true, message: '请输入商品价格', type:'number',trigger: 'blur' }
			],
			goodsUnit : [
				{ required: true, message: '请输入商品单位', trigger: 'blur' },
			    {min : 1, max : 3,message:'需要在1~3个字符或文字之间'}
			]
			}
		
	},
	mounted(){
		this.selectHouseForQuery();
		this.selectLocationForQuery();
	},
	methods : {
		beforeUpload(file){
			this.chooseFileBtn = true;
			const extension1 = file.name.split('.')[1] === 'xls';
			const isLt2M = file.size / 1024 / 1024 < 12;
			if(!extension1){
				this.$notify({
		            title: '警告',
		            message: this.tip1,
		            type: 'warning'
		          });
			}
			
			if(!isLt2M){
				this.$notify({
		            title: '警告',
		            message: this.tip2,
		            type: 'warning'
		          });
			}
			
			return extension1 && isLt2M;
			
		},
		uploadSuccess(response, file, fileList){
			this.chooseFileBtn = false;
		},
		selectHouseForQuery(){
			$.ajax({
				url : COMMON_URL.URL + 'housAndPositionController/getAllHouse',
				dataType : 'json',
				type : 'get',
				success : function(data){
					
					if(data.ok){
						outorderVue.house = data.data;
					}else{
						swal(data.msg,'','warning');
					}
					
				},
				error : function(data){
					swal('查询仓库失败','','error');
				}
			});
		},
		selectQueryPositionByHouse(val){
			
			
			if(val == null || val == ''){
				return;
			}
			
			$.ajax({
				url : COMMON_URL.URL + 'housAndPositionController/findPositionForQuery',
				dataType : 'json',
				type : 'get',
				data : {'houseId' : val},
				success : function(data){
					
					if(data.ok){
						outorderVue.position = data.data;
					}else{
						swal(data.msg,'','warning');
					}
				},
				error : function(data){
					swal('查询库位失败','','error');
				}
			});
			
		},
		selectLocationForQuery(){
			$.ajax({
				url : COMMON_URL.URL + "goodsLocationController/getGoodLocation",
				dataType : 'json',
				type : 'get',
				success : function(data){
					
					if(data.ok){
						outorderVue.location = data.data;
					}else{
						swal(data.msg,'','warning');
					}
					
				},
				error : function(data){
					swal('查询失败','','error');
				}
			});
		},
		handleCurrentChange(val){
			this.currentPage = val;
		},
		handleSizeChange(val){
			this.pageSize = val;
		},
		selectOrderHouseForQuery(val){
			if(val == null || val == ''){
				return;
			}
			
			let obj = {};
			obj = this.house.find((item)=>{
				return item.id === val;
			});
									
			this.createOutorderForm.toWhereHouseName = obj.houseName;
			this.selectQueryPositionByHouse(val);
		},
		selectOrderPositionForQuery(val){
			if(val == null || val == ''){
				return;
			}
			
			let obj = {};
			obj = this.position.find((item)=>{
				return item.id === val;
			});
			
			this.createOutorderForm.toWherePositionName = obj.positionName;
		},
		selectOrderLocationForQuery(val){
			
			if(val == null || val == ''){
				return;
			}
			
			let obj = {};
			obj = this.location.find((item)=>{
				return item.id === val;
			});
			
			this.createOutorderForm.toWhereLocationName = obj.locationName;
			this.queryGoodsByOrder();
		},
		queryGoodsByOrder(){
			
			var houseId = this.createOutorderForm.toWhereHouse;
			var positionId = this.createOutorderForm.toWherePosition;
			var locationId = this.createOutorderForm.toWhereLocation;
			if(houseId == null || houseId == ""){
				swal('必须选择仓库','','warning');
				return;
			}
			if(positionId == null || positionId == ""){
				swal('必须选择货位','','warning');
				return;
			}
			$.ajax({
				
				url : COMMON_URL.URL + 'goodsLocationController/getGoodsByHPL',
				type : 'get',
				dataType : 'json',
				data : {
					'houseId' : houseId,
					'positionId' : positionId,
					'locationId' : locationId
				},
				success : function(data){
					
					if(data.ok){
						outorderVue.goods = data.data;
					}else{
						swal(data.msg,'','warning');
					}
					
				},
				error : function(data){
					swal('系统异常','','error');
				}
				
			});
			
		},
		onQueryForBtn(){
			
			this.orderVo.pageNum = this.currentPage;
			this.orderVo.pageSize = this.pageSize;
			
			this.queryBtn = true;
			
			$.ajax({
				url : COMMON_URL.URL + 'orderManage/queryOutorderDetail',
				type : 'post',
				contentType : 'application/json',
				data : JSON.stringify(outorderVue.orderVo),
				success : function(data){
					outorderVue.queryBtn = false;
					if(data.ok){
						outorderVue.outorderDetail = data.data;
					}else{
						swal(data.msg,'','warning');
					}
					
				},
				error : function(data){
					outorderVue.queryBtn = false;
					swal('查询失败','','error');
				}
			});
			
		},
		restDialogForm(id){
			this.dialogFormVisible = false;
			this.$refs[id].resetFields();
		},
		submitFomr(id){
			this.dialogFormVisible = false;
			this.$refs[id].validate((valid)=>{
				if(valid){
					$.ajax({
						url : COMMON_URL.URL + 'orderManage/saveInorderForOne',
						type : 'POST',
						contentType : 'application/json',
						data : JSON.stringify(createOutorderForm),
						success : function(data){
							
							if(data.ok){
							}else{
								swal(data.msg,'','warning');
							}
						},
						error : function(data){
							swal('执行新增失败，请重新输入','','error');
						}
						
					});
				}
			});
			this.$refs[id].resetFields();
		},
		removeDomain(item) {
	        var index = this.createOutorderForm.goodsTrades.indexOf(item)
	        if (index !== -1) {
	          this.createOutorderForm.goodsTrades.splice(index, 1)
	        }
	      },
	    addDomain() {
	        this.createOutorderForm.goodsTrades.push({
	          id : '',
			  goodsNum : '',
	          key: Date.now()
	        });
	      }
		
	}
});